package com.bdmc.smokesensor.controller;


import java.util.List;
import java.util.Map;

import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.entity.Smk_Buildings;
import com.bdmc.smokesensor.entity.Smk_Devices;
import com.bdmc.smokesensor.entity.Smk_GroupCompany;
import com.bdmc.smokesensor.entity.Sys_Log;
import com.bdmc.smokesensor.iservices.ISmk_BuildingsService;
import com.bdmc.smokesensor.iservices.ISmk_DevicesService;
import com.bdmc.smokesensor.iservices.ISmk_GroupCompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/building",method = RequestMethod.POST)
public class Smk_BuildingsController extends BaseController{

    @Autowired
    private ISmk_BuildingsService _buildSvs;

    @Autowired
    private ISmk_DevicesService _devSvs;

    @Autowired
    private ISmk_GroupCompanyService _companySvs;

    @RequestMapping(value = "/list" )
    public Msg list(@RequestBody Map<String, Integer> reqMap) {
        int page =reqMap.get("page");
        int rows = reqMap.get("rows");
        int gc_id = reqMap.get("gc_id");
        return ResultUtil.success(_buildSvs.findByPageable(gc_id,page, rows));
    }

    @RequestMapping(value = "/listall" )
    public Msg listAll(@RequestBody Map<String, String> reqMap) {
        int page =Integer.valueOf(reqMap.get("page"));
        int rows =Integer.valueOf( reqMap.get("rows"));
        int gc_id = 0;
        if(reqMap.get("areaGcbd")!=null){
            gc_id = Integer.valueOf( reqMap.get("areaGcbd"));
        }
        
        String name = reqMap.get("name");
        int dep_id=Integer.valueOf(request.getAttribute("dep_id").toString());
        return ResultUtil.success(_buildSvs.findByDynamic(name,page, rows,dep_id,gc_id));
    }

    @RequestMapping(value = "getbyid")
    public Msg findOne(@RequestBody Map<String, Integer> reqMap) {
        int id =reqMap.get("id");
        return ResultUtil.success(_buildSvs.findOneById(id));
    }
    
    /**
     * 数据新增或更新，save方法可以执行添加也可以执行更新，如果需要执行持久化的实体存在主键值则更新数据，如果不存在则添加数据。
     */

    @RequestMapping(value = "/save" )
    public Msg save(@RequestBody Smk_Buildings bd) {
        String action = bd.getId()>0?"修改:":"新增:";
        if(!StringUtils.isEmpty(bd.getAreaGcbd())){
            String arrStr = bd.getAreaGcbd();
            String[] arr = arrStr.split(",");
            Integer gc_id = Integer.valueOf(arr[arr.length-1]);
            Smk_GroupCompany comp =_companySvs.findOneById(gc_id);
            bd.setSmkGroupCompanyId(gc_id);
            bd.setDepartmentId(comp.getDepartmentId());
        }
         Smk_Buildings b = _buildSvs.save(bd);
         if(b!=null&&b.getId()>0){
            Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "子单位", action+b.getName());
            _logSvs.save(log);
            return ResultUtil.success();
         }else{
            return ResultUtil.error(-1,"操作失败，请联系管理员。");
         }
         
        
    }

    @RequestMapping(value = "/delete")
    public Msg delete(@RequestBody Map<String, Integer> map) {
        int id = map.get("id");
        List<Smk_Devices> devs = _devSvs.findBySmkBuildingId(id);
        if(devs.size()>0){
            return ResultUtil.error(-1,"该子单位存在设备，无法删除。");
        }
        _buildSvs.deleteById(id);
        Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "子单位", "删除子单位："+id);
            _logSvs.save(log);
        return ResultUtil.success();
    }
    

}